﻿using ICSharpCode.SharpZipLib.Zip;
using Magicodes.ExporterAndImporter.Excel.AspNetCore;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NP.BPMReportPlatform.Entities.BPM_Platform;
using NP.BPMReportPlatform.MaterialMasterData.Dto;
using NP.BPMReportPlatform.RecurringJobs;
using NPOI.Util;
using Org.BouncyCastle.Asn1.BC;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.ObjectMapping;

namespace NP.BPMReportPlatform.MaterialMasterData
{
    /// <summary>
    /// 立才产品上架
    /// </summary>
    //[Authorize]
    [ApiController]
    [Route("api/mmd")]
    public class LBDXListingApplicationController : BPMReportPlatformController
    {
        private readonly ILBDXListingApplicationService _service;
        private readonly ILBDXRecurringJob _job;
        public LBDXListingApplicationController(ILBDXListingApplicationService service, ILBDXRecurringJob job)
        {
            _service = service;
            _job = job;
        }

        /// <summary>
        ///立才中心产品上架流程报表 查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("LBDXListingApplication/GetAllAsync")]
        public async Task<PagedResultDto<LBDXListingApplicationDto>> GetAllAsync([FromBody] LBDXListingApplicationInput input)
        {
            return await _service.GetAllAsync(input);
        }

        /// <summary>
        /// 立才中心产品上架流程报表 导出数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("LBDXListingApplication/ExportAsync")]
        public async Task<XlsxFileResult<LBDXListingApplicationDto>> ExportAsync([FromBody] LBDXListingApplicationInput input)
        {
            var rst = await _service.ExportAsync(input);
            return new XlsxFileResult<LBDXListingApplicationDto>(rst, "导出Excel");
        }

        [HttpPost]
        [Route("LBDXListingApplication/DownLoadFiles")]
        public async Task<FileResult> DownLoadFiles(DownLoadFileInput input)
        {
            var result = await _service.DownLoadFiles(input);
            return File(result.ToArray(), "application/octet-stream", $"{Guid.NewGuid()}.zip");
        }

        [HttpPost]
        [Route("LBDXListingApplication/FixData")]
        public void FixData(string instanceNumber, bool isReUpdate = false)
        {
            _service.FixData(instanceNumber, isReUpdate);
        }

        /// <summary>
        /// 同步课程数据到CourseBase表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("LBDXListingApplication/SyncDataToCourseBase")]
        public async Task SyncDataToCourseBase()
        {
            await _job.SyncDataToCourseBase();
        }
    }
}
